#include "occipool.hpp"
#include "occihandler.hpp"
#include <string>
using namespace com_oocl_riley;
using std::string;

OcciHandler::OcciHandler(OcciPool &pool)
: _pool(pool), _con(nullptr) {
    _con = _pool.getConnection();
}

OcciHandler::~OcciHandler() {
    _pool.releaseConnection(_con);
}

Result * OcciHandler::perform(Profile *profile) {
    DataCollection *data = new DataCollection;
    Result *result=new Result;
    result->warehouse=profile->Warehouse();
    result->owner=profile->Owner();
    result->profileName=profile->ProfileName();
    result->dataCollection=data;
    Statement *stmt = nullptr;
    ResultSet *rs = nullptr;
    string sql="select v.message_location,v.update_time,v.sourcefile_size from dmtp_auditrecord v where v.profile_name='";
    sql+=profile->ProfileName()+"'";
    stmt = _con->createStatement();
    rs = stmt->executeQuery();
    while (rs->next()) {
        string fileName = rs->getString(1);
        string dateTime = rs->getString(2);
        size_t fileSize = rs->getInt(3);
        data->insert(fileName, ValueType(dateTime, fileSize));
    }
    stmt->closeResultSet(rs);
    _con->terminateStatement(stmt);
    return result;
}
